package chap02.Array;

/**
 * Created by root on 2017/4/26.
 */
public class ArrayApp {
    public static void main(String[] args){
        long[] arr;    // reference to arr   引用
        arr = new long[100];
        int nElems = 0;
        int j;
        long searchKey;

        // insert 10 items
        arr[0] = 77;
        arr[1] = 99;
        arr[2] = 44;
        arr[3] = 55;
        arr[4] = 22;
        arr[5] = 88;
        arr[6] = 11;
        arr[7] = 00;
        arr[8] = 66;
        arr[9] = 33;
        nElems = 10;

        // display items
        for (j=0; j<nElems; j++) {
            System.out.print(arr[j] + " ");
        }
        System.out.println();

        // find item with key 66
        searchKey = 66;
        for (j=0; j<nElems; j++) {
            if (arr[j] == searchKey) {
                break;
            }
        }
        if (j == nElems) {
            System.out.println("can't find " + searchKey);
        } else {
            System.out.println("Found " + searchKey);
        }

        // delete item with key 55
        searchKey = 55;
        for (j=0; j<nElems; j++) {
            if (arr[j] == searchKey) {
                break;
            }
        }
        for (int k=j; k<nElems-1; k++) {
            arr[k] = arr[k+1];
        }
        nElems--;

        // display items
        for (j=0; j<nElems; j++) {
            System.out.print(arr[j] + " ");
        }
        System.out.println();
    }
}
